// pages/myCoupons/myCoupons.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    choose_modal: "none",
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navH: app.globalData.navHeight,
      productid: options.productid,
      usergiftcardid: options.usergiftcardid,
      type: options.type,
      thumbnailimage: options.thumbnailimage,
    });
    
  },
  /**
     * 页面分享
     */
  onShareAppMessage: function (e) {
    var that = this;
    console.info(that.data.usergiftcardid+","+wx.getStorageSync("userId"));
    that.giving();
    return {
      title: '极味生鲜',
      desc: '礼品卡赠送',
      imageUrl: that.data.thumbnailimage,
      path: '/pages/mallPage/mallPage?partnerUserId=' + wx.getStorageSync("userId") + '&giving=1&usergiftcardid=' + that.data.usergiftcardid + '&partnerUserName=' + wx.getStorageSync("nickName")+'&price='+that.data.product.price, // 路径，传递参数到指定页面
      success(res) {
        console.info(res);
        console.log("分享成功");
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    wx.request({
      url: app.globalData.url + "getProductById",
      data: {
        productId: that.data.productid
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          product: res.data.data
        })
      },
    });
    wx.request({
      url: app.globalData.url + "getUserAddressList",
      data: {
        userId: wx.getStorageSync("userId"),
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          addressList: res.data.data
        })
      },
    });
    

    wx.request({
      url: app.globalData.url + 'userGiftCard',
      method: 'GET',
      data: {
        userGiftCardId: that.data.usergiftcardid,
      },
      success: function (res) {
        that.setData({
          sendOutTime: res.data.data.sendOutTime,
          usedTime: res.data.data.usedTime,
          receivedTime: res.data.data.receivedTime,
        })
      },
    });
      var that = this;
      this.dialog = this.selectComponent("#dialog");
    },
    showDialog(e) {
      var that = this;
      that.setData({
        wholeaddress: e.currentTarget.dataset.wholeaddress,
        addrid: e.currentTarget.dataset.addrid,
      })
      this.dialog.showDialog();
    },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onReady();

   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  navBack: function () {
    var that = this;
    // var pages = getCurrentPages();
    // var prevPage = pages[pages.length - 2]; //上一个页面
    // //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    // prevPage.setData({
    //   type: that.data.type
    // })
    wx.navigateBack({//返回
      delta: 1
    })
  },
  giving:function(e){
    var that = this;
    wx.request({
      url: app.globalData.url + 'userGiveGiftCard',
      method: 'GET',
      data: {
        userGiftCardId: that.data.usergiftcardid,
      },
      success: function (res) {
        if (res.data.result) {
          wx.showToast({
            title: '赠送成功',
            icon: 'none',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
            duration: 2000,
          })
          wx.request({
            url: app.globalData.url + 'userGiftCard',
            method: 'GET',
            data: {
              userGiftCardId: that.data.usergiftcardid,
            },
            success: function (res) {
              that.setData({
                sendOutTime: res.data.data.sendOutTime,
                usedTime: res.data.data.usedTime, 
                receivedTime: res.data.data.receivedTime,
              })
            },
          });
        }

      },
    });
    // wx.showModal({
    //   title: '提示',
    //   content: '确定要赠送吗？',
    //   success: function (sm) {
    //     if (sm.confirm) {
    //       // 用户点击了确定 可以调用取消订单的方法了
         
    //     }
    //   }
    // });
    
  },
  // 弹出
  modal_show: function (e) {
    var that = this;
    that.setData({
      choose_modal: "block",
    });
  },
  //消失
  modal_none: function () {
    this.setData({
      choose_modal: "none",
    });
  },
  chooseAddr:function(){

  },
  //取消事件
  _cancelEvent() {
    console.log('你点击了取消');
    this.dialog.hideDialog();
  },
  //确认事件
  _confirmEvent() {
    console.log('你点击了确定');
    var that = this;
    this.dialog.hideDialog();
    wx.request({
      url: app.globalData.url + 'submitOrder',
      method: 'GET',
      data: {
        type: 6,
        giftCardWay:2,
        addressId: that.data.addrid,
        userCardId: that.data.usergiftcardid,
        price:that.data.product.price,
        userId:wx.getStorageSync("userId"),
        payChannel: 5003
      },
      success: function (res) {
        that.modal_none();
        wx.request({
          url: app.globalData.url + 'userGiftCard',
          method: 'GET',
          data: {
            userGiftCardId: that.data.usergiftcardid,
          },
          success: function (res) {
            that.setData({
              sendOutTime: res.data.data.sendOutTime,
              usedTime: res.data.data.usedTime,
              receivedTime: res.data.data.receivedTime,
            })
          },
        });
      },
    });
  },
  mentionCargo:function(){
    wx.navigateTo({
      url: '/pages/mentionCargo/mentionCargo',
    })
  },
  toAddress:function(){
    this.modal_none();
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },
  undo: function() {
    var that= this;
    wx.request({
      url: app.globalData.url + 'userRevokeGiveGiftCard',
      method: 'GET',
      data: {
        userGiftCardId: that.data.usergiftcardid,
      },
      success: function (res) {
        if (res.data.result == 1) {
         
          wx.showToast({
            title: '撤销成功',
            icon: 'none',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
            duration: 2000,
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            });
          }, 2000)
          // wx.request({
          //   url: app.globalData.url + 'userGiftCard',
          //   method: 'GET',
          //   data: {
          //     userGiftCardId: that.data.usergiftcardid,
          //   },
          //   success: function (res) {
          //     that.setData({
          //       sendOutTime: res.data.data.sendOutTime,
          //       usedTime: res.data.usedTime,
          //       receivedTime: res.data.receivedTime,
          //     })
          //   },
          // });
        } else {
          wx.showToast({
            title: res.data.errmsg,
            icon: 'none',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
            duration: 2000,
          })
        }
      }, 
    });
  },
})